<template>
  <el-dialog title="新增" :visible.sync="addStudentDialogVisible" :close-on-click-modal="false" :before-close="close" width="500px">
    <div id="addStudent">
      <el-form label-position="left" :model="addStudent" ref="addStudent" label-width="80px" :rules="rules">
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="addStudent.name" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="学生学号" prop="number">
          <el-input v-model="addStudent.number" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="addStudent.gender" placeholder="请选择性别" @change="getDormitory">
            <el-option
              v-for="item in genderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="grade">
          <el-cascader
            v-model="addStudent.grade"
            placeholder="请选择班级"
            :options="gradeOptions"
            :props="{expandTrigger: 'hover',label:'name',value:'id'}">
          </el-cascader>
        </el-form-item>
        <el-form-item label="宿舍" prop="dormitory">
          <el-cascader
            v-model="addStudent.dormitory"
            placeholder="请选择宿舍"
            :options="dormotoryOptions"
            :props="{ expandTrigger: 'hover',label:'name',value:'id' }">
          </el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveStudent">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
  import student from '@/api/student/student'
  import grade from '@/api/grade/grade'
  import dormitory from '@/api/dormitory/dormitory'
  export default {
    name: 'AddStudent',
    props: ['addStudentDialogVisible'],
    data() {
      return {
        genderOptions: [
          {
            label: '男孩',
            value: 0
          },
          {
            label: '女孩',
            value: 1
          }
        ],
        gradeOptions: [],
        dormotoryOptions:[],
        addStudent: {},
        rules: {
          name: [
            { required: true, message: '请输入学生姓名', trigger: 'blur' },
            { min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '请输入学生学号', trigger: 'change' }
          ],
          gender: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          grade: [
            { required: true, message: '请选择班级', trigger: 'change' }
          ],
          dormitory: [
            { required: true, message: '请选择宿舍', trigger: 'change' }
          ]
        }
      }
    },
    created() {

      grade.getGradeAndClazz().then(res=>{
        this.gradeOptions = res.data
      })
    },
    methods: {
      saveStudent() {
        this.$refs["addStudent"].validate(valid=>{
          if (valid){
            var studentInfo = {
              name:this.addStudent.name,
              number:this.addStudent.number,
              gender:this.addStudent.gender,
              gradeId: this.addStudent.grade[0],
              clazzId:this.addStudent.grade[1],
              buildingId:this.addStudent.dormitory[0],
              dormitoryId:this.addStudent.dormitory[1]
            }
            student.addStudent(studentInfo)
              .then(()=>{
                this.$message.success("添加成功")
                this.$parent.getStudents()
                this.close()
              }).catch(()=>{
                this.close()
            })
          }else {
            this.$message.error("请正确填写信息")
            return false
          }
        })
      },
      /*查询寝室信息*/
      getDormitory(genderId){
        dormitory.getDormitoryByGender(genderId).then(res=>{
          this.dormotoryOptions = res.data
          console.log(this.dormotoryOptions)
        })
      },
      /*关闭dialog*/
      close() {
        this.$refs['addStudent'].resetFields()
        this.$parent.closeDialog()
      }
    }
  }
</script>

<style scoped>
</style>
